Skip to content

feat: support multiple value for pivot #1970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

chenkovsky
Copy link
Contributor

https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select-pivot.html

currently this sql is not supported.

SELECT * FROM person
    PIVOT (
        SUM(age) AS a, AVG(class) AS c
        FOR (name, age) IN (('John', 30) AS c1, ('Mike', 40) AS c2)
    );

src/ast/query.rs Outdated
@@ -1336,7 +1336,7 @@ pub enum TableFactor {
Pivot {
table: Box<TableFactor>,
aggregate_functions: Vec<ExprWithAlias>, // Function expression
value_column: Vec<Ident>,
value_column: Vec<Expr>, // Expr is a identifier or a compound identifier
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value_column: Vec<Expr>, // Expr is a identifier or a compound identifier
value_column: Vec<Expr>,

Comment on lines 10933 to 10936
assert_eq!(
verified_stmt(sql_with_multiple_value_column).to_string(),
sql_with_multiple_value_column
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm this assertion is probably not needed, I think verified_stmt already does the same assertion

Comment on lines 13843 to 13849
let value_column = if self.peek_token_ref().token == Token::LParen {
self.parse_parenthesized_compound_identifier_list(Mandatory, false)?
} else {
vec![Expr::CompoundIdentifier(
self.parse_period_separated(|p| p.parse_identifier())?,
)]
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to unpivot can we call parse_expr directly here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, we cannot do it here. (name, age) IN (('John', 30) AS c1, ('Mike', 40) AS c2) will be parsed as an expr.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right that makes sense, I think we can do something this instead to reuse existing capabilities of the parser?

let value_column = if self.peek_token_ref().token == Token::LParen {
    self.parse_parenthesized_column_list_inner(Mandatory, false, |p| {
         p.parse_subexpr(self.dialect.prec_value(Precedence::Between))
     })?
} else {
    vec![self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?]
};

Comment on lines 13843 to 13849
let value_column = if self.peek_token_ref().token == Token::LParen {
self.parse_parenthesized_compound_identifier_list(Mandatory, false)?
} else {
vec![Expr::CompoundIdentifier(
self.parse_period_separated(|p| p.parse_identifier())?,
)]
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right that makes sense, I think we can do something this instead to reuse existing capabilities of the parser?

let value_column = if self.peek_token_ref().token == Token::LParen {
    self.parse_parenthesized_column_list_inner(Mandatory, false, |p| {
         p.parse_subexpr(self.dialect.prec_value(Precedence::Between))
     })?
} else {
    vec![self.parse_subexpr(self.dialect.prec_value(Precedence::Between))?]
};

@@ -11143,7 +11146,7 @@ fn parse_pivot_unpivot_table() {
expr: call("sum", [Expr::Identifier(Ident::new("population"))]),
alias: None
}],
value_column: vec![Ident::new("year")],
value_column: vec![Expr::CompoundIdentifier(vec![Ident::new("year")])],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm heads up that this representation wouldn't be ideal - a compound identifier would be expected to have more than one ident in it

@@ -10875,7 +10875,10 @@ fn parse_pivot_table() {
expected_function("b", Some("t")),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add tests to demonstrate the new behavior? it seems like those are currently lacking in the PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants